/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.plc4x.java.profinet.readwrite;

import static org.apache.plc4x.java.spi.codegen.fields.FieldReaderFactory.*;
import static org.apache.plc4x.java.spi.codegen.fields.FieldWriterFactory.*;
import static org.apache.plc4x.java.spi.codegen.io.DataReaderFactory.*;
import static org.apache.plc4x.java.spi.codegen.io.DataWriterFactory.*;
import static org.apache.plc4x.java.spi.generation.StaticHelper.*;

import java.time.*;
import java.util.*;
import org.apache.plc4x.java.api.exceptions.*;
import org.apache.plc4x.java.api.value.*;
import org.apache.plc4x.java.spi.codegen.*;
import org.apache.plc4x.java.spi.codegen.fields.*;
import org.apache.plc4x.java.spi.codegen.io.*;
import org.apache.plc4x.java.spi.generation.*;

// Code generated by code-generation. DO NOT EDIT.

public abstract class PnDcp_Block implements Message {

  // Abstract accessors for discriminator values.
  public abstract PnDcp_BlockOptions getOption();

  public abstract Short getSuboption();

  public PnDcp_Block() {
    super();
  }

  protected abstract void serializePnDcp_BlockChild(WriteBuffer writeBuffer)
      throws SerializationException;

  public void serialize(WriteBuffer writeBuffer) throws SerializationException {
    PositionAware positionAware = writeBuffer;
    int startPos = positionAware.getPos();
    writeBuffer.pushContext("PnDcp_Block");

    // Discriminator Field (option) (Used as input to a switch field)
    writeDiscriminatorEnumField(
        "option",
        "PnDcp_BlockOptions",
        getOption(),
        new DataWriterEnumDefault<>(
            PnDcp_BlockOptions::getValue,
            PnDcp_BlockOptions::name,
            writeUnsignedShort(writeBuffer, 8)));

    // Discriminator Field (suboption) (Used as input to a switch field)
    writeDiscriminatorField("suboption", getSuboption(), writeUnsignedShort(writeBuffer, 8));

    // Implicit Field (blockLength) (Used for parsing, but its value is not stored as it's
    // implicitly given by the objects content)
    int blockLength = (int) ((getLengthInBytes()) - (4));
    writeImplicitField("blockLength", blockLength, writeUnsignedInt(writeBuffer, 16));

    // Switch field (Serialize the sub-type)
    serializePnDcp_BlockChild(writeBuffer);

    writeBuffer.popContext("PnDcp_Block");
  }

  @Override
  public int getLengthInBytes() {
    return (int) Math.ceil((float) getLengthInBits() / 8.0);
  }

  @Override
  public int getLengthInBits() {
    int lengthInBits = 0;
    PnDcp_Block _value = this;

    // Discriminator Field (option)
    lengthInBits += 8;

    // Discriminator Field (suboption)
    lengthInBits += 8;

    // Implicit Field (blockLength)
    lengthInBits += 16;

    // Length of sub-type elements will be added by sub-type...

    return lengthInBits;
  }

  public static PnDcp_Block staticParse(ReadBuffer readBuffer, Object... args)
      throws ParseException {
    PositionAware positionAware = readBuffer;
    return staticParse(readBuffer);
  }

  public static PnDcp_Block staticParse(ReadBuffer readBuffer) throws ParseException {
    readBuffer.pullContext("PnDcp_Block");
    PositionAware positionAware = readBuffer;
    int startPos = positionAware.getPos();
    int curPos;

    PnDcp_BlockOptions option =
        readDiscriminatorField(
            "option",
            new DataReaderEnumDefault<>(
                PnDcp_BlockOptions::enumForValue, readUnsignedShort(readBuffer, 8)));

    short suboption = readDiscriminatorField("suboption", readUnsignedShort(readBuffer, 8));

    int blockLength = readImplicitField("blockLength", readUnsignedInt(readBuffer, 16));

    // Switch Field (Depending on the discriminator values, passes the instantiation to a sub-type)
    PnDcp_BlockBuilder builder = null;
    if (EvaluationHelper.equals(option, PnDcp_BlockOptions.IP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 1)) {
      builder = PnDcp_Block_IpMacAddress.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.IP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 2)) {
      builder = PnDcp_Block_IpParameter.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.IP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 3)) {
      builder = PnDcp_Block_FullIpSuite.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 1)) {
      builder =
          PnDcp_Block_DevicePropertiesDeviceVendor.staticParseBuilder(readBuffer, blockLength);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 2)) {
      builder =
          PnDcp_Block_DevicePropertiesNameOfStation.staticParseBuilder(readBuffer, blockLength);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 3)) {
      builder = PnDcp_Block_DevicePropertiesDeviceId.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 4)) {
      builder = PnDcp_Block_DevicePropertiesDeviceRole.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 5)) {
      builder =
          PnDcp_Block_DevicePropertiesDeviceOptions.staticParseBuilder(readBuffer, blockLength);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 6)) {
      builder = PnDcp_Block_DevicePropertiesAliasName.staticParseBuilder(readBuffer, blockLength);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 7)) {
      builder = PnDcp_Block_DevicePropertiesDeviceInstance.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 8)) {
      builder = PnDcp_Block_DevicePropertiesOemDeviceId.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_PROPERTIES_OPTION)
        && EvaluationHelper.equals(suboption, (short) 9)) {
      builder = PnDcp_Block_DevicePropertiesStandardGateway.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 12)) {
      builder = PnDcp_Block_DhcpOptionHostName.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 43)) {
      builder = PnDcp_Block_DhcpOptionVendorSpecificInformation.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 54)) {
      builder = PnDcp_Block_DhcpOptionServerIdentifier.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 55)) {
      builder = PnDcp_Block_DhcpOptionParameterRequestList.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 60)) {
      builder = PnDcp_Block_DhcpOptionClassIdentifier.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 61)) {
      builder = PnDcp_Block_DhcpOptionDhcpClientIdentifier.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 81)) {
      builder = PnDcp_Block_DhcpOptionFullyQualifiedDomainName.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DCP_OPTION)
        && EvaluationHelper.equals(suboption, (short) 97)) {
      builder = PnDcp_Block_DhcpOptionUuidBasedClient.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.CONTROL_OPTION)
        && EvaluationHelper.equals(suboption, (short) 1)) {
      builder = PnDcp_Block_ControlOptionStart.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.CONTROL_OPTION)
        && EvaluationHelper.equals(suboption, (short) 2)) {
      builder = PnDcp_Block_ControlOptionStop.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.CONTROL_OPTION)
        && EvaluationHelper.equals(suboption, (short) 3)) {
      builder = PnDcp_Block_ControlOptionSignal.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.CONTROL_OPTION)
        && EvaluationHelper.equals(suboption, (short) 4)) {
      builder = PnDcp_Block_ControlOptionResponse.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.CONTROL_OPTION)
        && EvaluationHelper.equals(suboption, (short) 5)) {
      builder = PnDcp_Block_ControlOptionFactoryReset.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.CONTROL_OPTION)
        && EvaluationHelper.equals(suboption, (short) 6)) {
      builder = PnDcp_Block_ControlOptionResetToFactory.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.DEVICE_INITIATIVE_OPTION)
        && EvaluationHelper.equals(suboption, (short) 1)) {
      builder = PnDcp_Block_DeviceInitiativeOption.staticParseBuilder(readBuffer);
    } else if (EvaluationHelper.equals(option, PnDcp_BlockOptions.ALL_SELECTOR_OPTION)
        && EvaluationHelper.equals(suboption, (short) 0xFF)) {
      builder = PnDcp_Block_ALLSelector.staticParseBuilder(readBuffer);
    }
    if (builder == null) {
      throw new ParseException(
          "Unsupported case for discriminated type"
              + " parameters ["
              + "option="
              + option
              + " "
              + "suboption="
              + suboption
              + "]");
    }

    readBuffer.closeContext("PnDcp_Block");
    // Create the instance
    PnDcp_Block _pnDcp_Block = builder.build();
    return _pnDcp_Block;
  }

  public static interface PnDcp_BlockBuilder {
    PnDcp_Block build();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof PnDcp_Block)) {
      return false;
    }
    PnDcp_Block that = (PnDcp_Block) o;
    return true;
  }

  @Override
  public int hashCode() {
    return Objects.hash();
  }

  @Override
  public String toString() {
    WriteBufferBoxBased writeBufferBoxBased = new WriteBufferBoxBased(true, true);
    try {
      writeBufferBoxBased.writeSerializable(this);
    } catch (SerializationException e) {
      throw new RuntimeException(e);
    }
    return "\n" + writeBufferBoxBased.getBox().toString() + "\n";
  }
}
